/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.displaytag.properties.SortOrderEnum;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.ReportDao;
import org.istt.domain.Report;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * @author Ndtho
 * 
 */
@Repository("reportDao")
public class ReportDaoHibernate extends BaseDaoHibernate<Report, Long> implements ReportDao {
	@Autowired
	public ReportDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Report> getReportList(final int firstRecordIndex, final int pageSize, 
			final SortOrderEnum sortDirection, final String sortCriterion) {
		return (List<Report>) hibernateTemplate.execute(new HibernateCallback<Object>() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Report.class);
				
				return criteria.setFirstResult(firstRecordIndex).setMaxResults(pageSize).list();
			}
		});
	}
	
	@Override
	public int getRecordCount() {
		return hibernateTemplate.execute(new HibernateCallback<Integer>() {
		// TODO Auto-generated method stub
			@Override
			public Integer doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Report.class);
				criteria.setProjection(Projections.rowCount());
				return ((Long)criteria.uniqueResult()).intValue();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Report> searchReport(final String name,final int firstRecordIndex,
			final int pageSize,final SortOrderEnum sortDirection,final String sortCriterion) {
		// TODO Auto-generated method stub
		return (List<Report>) hibernateTemplate.execute(new HibernateCallback<Object>() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Report.class);
				if(name!=null && !"".equals(name)){
					criteria.add(Restrictions.like("reportName","%" + name + "%"));
				}
				
				List<Report> result = criteria.setFirstResult(firstRecordIndex).setMaxResults(pageSize).list();
				return result;
			}
		});
	}

	@Override
	public int getSearchRecordCount(final String name) {
		// TODO Auto-generated method stub
		return (Integer) hibernateTemplate.execute(new HibernateCallback<Object>() {

			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				
				Criteria cri = session.createCriteria(Report.class);

				if (!StringUtils.isEmpty(name)) {
					cri.add(Restrictions.like("reportName", name));
				}
				
				return ((Long)cri.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			}
		});
	}
}
